package com.minivision.sdktestdemo.ui.view;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by devtang on 2016/5/19.
 */
public abstract class MusicCtrlPopContentViewImpl implements PopContentView {
    private View mContentView;

    private Activity mContext;

    private PopupWindow mPopupWindow;

    @Bind(R.id.tv_title)
    TextView mTvTitle;

    @Bind(R.id.pop_click_item_list)
    RecyclerView mRecyclerView;

    private MusicCtrlPopItemRecyclerAdapter mAdapter;

    private List<PopClickItemFactory.PopClickItem> mList;

    public MusicCtrlPopContentViewImpl(Activity context) {
        mContext = context;
        ButterKnife.bind(this, getContentView());
        initList();
    }

    @Override
    public int getMaxPopWindowHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    @Override
    public View getContentView() {
        if (mContentView == null) {
            mContentView = mContext.getLayoutInflater().inflate(R.layout.pop_ctrl_view, null);
        }
        return mContentView;
    }

    @Override
    public void dismiss() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    @Override
    public void setPopupWindow(PopupWindow popupWindow) {
        mPopupWindow = popupWindow;
    }

    @Override
    public Activity getContext() {
        return mContext;
    }

    protected void addItemToList(PopClickItemFactory.PopClickItem item) {
        if (mList == null) {
            mList = new ArrayList<>();
        }
        mList.add(item);
    }

    protected List<PopClickItemFactory.PopClickItem> getClickList() {
        return mList;
    }

    protected abstract List<PopClickItemFactory.PopClickItem> getPopClickList();

    private void initList() {
        mAdapter = new MusicCtrlPopItemRecyclerAdapter(getPopClickList());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);
    }

    protected void setTitle(String text) {
        mTvTitle.setText(text);
    }

}
